import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export const useTasklistStore = defineStore('counter', () => {
  var tasklist = ref([])
  // 统计未完成的任务
  var unFinishedCount = computed(()=>{
   
    return tasklist.value.filter((item)=>!item.status).length;
  })
  //  新增任务
  var addTask = (payload) => {
    tasklist.value.push({ id: tasklist.value.length + 1, name: payload, status: false })
  }
  // 改变任务状态
  var changeStatus = (payload) => {
    var task = tasklist.value.find((item) => {
      return item.id == payload;
    })
    if (task) {
      task.status = !task.status
    }
  }
  // 删除
  var deleteTask = (payload) => {
    var index = tasklist.value.findIndex((item) => {
      return item.id == payload
    })
    if (index != -1) {
      tasklist.value.splice(index, 1)
    }
  }
  return {
    tasklist,
    addTask,
    changeStatus,
    deleteTask,
    unFinishedCount,
  }
})

